package com.iflytek.elpmobile.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileUtils
{
    public static long getFileOrDirectorySize(File f)
    {
        if (f == null)
        {
            return 0;
        }

        if (!f.isDirectory())
        {
            return f.length();
        }
        else
        {
            File[] files = f.listFiles();
            if (files == null)
            {
                return 0;
            }

            long totalSize = 0;
            for (File file : files)
            {
                totalSize += getFileOrDirectorySize(file);// 递归。
            }
            return totalSize;
        }
    }
    
    public static void deleteDirectory(File directory) throws IOException
    {
    	 cleanDirectory(directory);

         if (!directory.delete())
         {
             String message = "Unable to delete directory " + directory + ".";
             throw new IOException(message);
         }
    }
    
    public static void deleteFile(File file) throws IOException{
    	if (!file.exists())
        {
//            String message = file + " does not exist";
//            throw new IllegalArgumentException(message);
    		return;
        }

        if (!file.isFile())
        {
            String message = file + " is not a File";
            throw new IllegalArgumentException(message);
        }
        file.delete();
    }
    
    public static long getFileSize(File file) {
		long fileSize = 0;
		FileInputStream fis = null;
		if (file.exists()) {
			try {
				fis = new FileInputStream(file);
				fileSize = fis.available();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (fis != null) {
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return fileSize;
	}
    private static void cleanDirectory(File directory) throws IOException
    {
        if (!directory.exists())
        {
            String message = directory + " does not exist";
            throw new IllegalArgumentException(message);
        }

        if (!directory.isDirectory())
        {
            String message = directory + " is not a directory";
            throw new IllegalArgumentException(message);
        }

        File[] files = directory.listFiles();
        if (files == null)
        { // null if security restricted
            throw new IOException("Failed to list contents of " + directory);
        }

        IOException exception = null;
        for (File file : files)
        {
            try
            {
                forceDelete(file);
            }
            catch (IOException ioe)
            {
                exception = ioe;
            }
        }

        if (null != exception)
        {
            throw exception;
        }
    }
    
    private static void forceDelete(File file) throws IOException
    {
        if (file.isDirectory())
        {
            deleteDirectory(file);
        }
        else
        {
            boolean filePresent = file.exists();
            if (!file.delete())
            {
                if (!filePresent)
                {
                    throw new FileNotFoundException("File does not exist: "
                            + file);
                }
                String message = "Unable to delete file: " + file;
                throw new IOException(message);
            }
        }
    }
}